var FGScheck = undefined;

var FGSoperator = {
	port: false,
	postMessage: function(data) {
		if(this.port == false)
		{
			data.response = 
			{
				success: false,
				data: ''
			};
			
			FGS.processOperatorMessage(data);
		}
		else
		{
			for(var i =0; i<data.params.length;i++)
			{
				if(typeof data.params[i] == 'function')
				{
					data.params[i] = data.params[i].toString()
				}
			}
			this.port.postMessage(data);
		}
	}
};

function checkFGSoperator() 
{
	if( FGSoperator.port == false )
	{
		var iframe = document.getElementById("vboxForIframe");
		if(iframe)
		{
			iframe.setAttribute("src", "about:blank");
			iframe.setAttribute("src", "http://www.facebook.com/ajax/ufi/modify.php?x=y");
		}
	}
	setTimeout(checkFGSoperator, 3000);
}

var FGSdebugMode = false;

window.addEventListener("load", function(e) { FGS.preStartup(e); }, false);
window.document.addEventListener("DOMContentLoaded",function(e) { FGS.checkIfFacebookPage(e); }, false);

const MY_EXTENSION_UUID = "friendlygamingsimplifier@flies";
const OBSERVER = Services.obs;
const ADDONMANAGER = AddonManager;

var FGSlistener = {

	beingUninstalled: false,

	onLoad: function() {
		this.register();
	},

	observe: function(subject, topic, data)
	{
		if (topic == "quit-application-granted")
		{
			var prefs = Components.classes["@mozilla.org/preferences-service;1"]
								.getService(Components.interfaces.nsIPrefService);
			prefs = prefs.getBranch("extensions.FGS.");
			prefs.setBoolPref("uninstall", this.beingUninstalled);
			
			this.unregister();
		}
	},

	onUninstalling: function(addon) {
		if (addon.id == MY_EXTENSION_UUID) {
			this.beingUninstalled = true;
		}
	},

	onOperationCancelled: function(addon) {
		if (addon.id == MY_EXTENSION_UUID) {
			this.beingUninstalled = (addon.pendingOperations & AddonManager.PENDING_UNINSTALL) != 0;
		}
	},  

	register: function()
	{
		OBSERVER.addObserver(this, "quit-application-granted", false);
		ADDONMANAGER.addAddonListener(this);
	},

	unregister: function()
	{
		OBSERVER.removeObserver(this, "quit-application-granted");
		ADDONMANAGER.removeAddonListener(this);
	}
};

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/AddonManager.jsm");